home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 293_02 / pictga.c < prev    next >
C/C++ Source or Header  |  1989-08-23  |  4KB  |  155 lines

  1. /*
  2.  * Converts a 256*n B/W picture file into a 256*256 TARGA-TIPS B/W or RGB file.
  3.  *
  4.  * Usage: pictga infile outfile [-c]
  5.  *        -c   : color image RGB (blue background)
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <ctype.h>
  10. #include <math.h>
  11. #ifdef MSDOS
  12. typedef int WORD;
  13. #else
  14. typedef short int WORD;
  15. #endif
  16. #ifndef SEEK_END
  17. #define SEEK_END 2
  18. #endif
  19.  
  20. /* allocate all arrays as global */
  21. char  fni[256], fno[256];
  22. unsigned char inline[256];
  23. unsigned char outrgb[256][3];
  24.  
  25.  
  26. main(argc, argv)
  27. int argc;
  28. char *argv[];
  29. {
  30.   FILE *fpi, *fpo;
  31.   WORD  tgaheadi;
  32.   unsigned char tgaheadc;
  33.   int   line, pixel;
  34.   int   nlines;
  35.   int   rgb;
  36.  
  37.   fni[0] = fno[0] = '\0'; 
  38.   rgb = 0;
  39.   for (argc; argc > 1; argc--) {
  40.     if (**++argv == '-') {
  41.       switch (tolower(*(*argv + 1))) {
  42.         case 'c':
  43.           rgb = 1;
  44.           break;
  45.         default:
  46.           printf("Usage: pictga infile outfile [-c]\n");
  47.           exit(0);
  48.           break;
  49.       }
  50.     }
  51.     else {
  52.       if (fni[0] == '\0')
  53.         strcpy(fni, *argv);
  54.       else
  55.         strcpy(fno, *argv);
  56.     }
  57.   }
  58.  
  59.   if (fni[0] == '\0' || fno[0] == '\0') {
  60.     printf("Usage: pictga infile outfile [-c]\n");
  61.     exit(0);
  62.   }
  63.  
  64.   if ((fpi = fopen(fni, "rb")) == NULL) {
  65.     printf("Error opening %s\n", fni);
  66.     exit(1);
  67.   }
  68.   if ((fpo = fopen(fno, "wb")) == NULL) {
  69.     printf("Error opening %s\n", fno);
  70.     exit(1);
  71.   }
  72.  
  73.   /* find number of lines in input file */
  74.   fseek(fpi, 0, SEEK_END);
  75.   nlines = ftell(fpi) / 256;
  76.   printf("Number of lines in input = %d\n", nlines);
  77.   rewind(fpi);
  78.  
  79.   /* write header to tga file */
  80.   tgaheadc = 0;
  81.   fwrite(&tgaheadc, sizeof(unsigned char), 1, fpo);
  82.   fwrite(&tgaheadc, sizeof(unsigned char), 1, fpo);
  83.   tgaheadc = (rgb) ? 3 : 2;
  84.   fwrite(&tgaheadc, sizeof(unsigned char), 1, fpo);
  85.   tgaheadc = 0;
  86.   fwrite(&tgaheadc, sizeof(unsigned char), 1, fpo);
  87.   fwrite(&tgaheadc, sizeof(unsigned char), 1, fpo);
  88.   fwrite(&tgaheadc, sizeof(unsigned char), 1, fpo);
  89.   fwrite(&tgaheadc, sizeof(unsigned char), 1, fpo);
  90.   fwrite(&tgaheadc, sizeof(unsigned char), 1, fpo);
  91.   tgaheadi = 0;
  92.   fwrite(&tgaheadi, sizeof(WORD), 1, fpo);
  93.   fwrite(&tgaheadi, sizeof(WORD), 1, fpo);
  94.   tgaheadi = 256;
  95.   fwrite(&tgaheadi, sizeof(WORD), 1, fpo);
  96.   fwrite(&tgaheadi, sizeof(WORD), 1, fpo);
  97.   tgaheadc = (rgb) ? 24 : 8;
  98.   fwrite(&tgaheadc, sizeof(unsigned char), 1, fpo);
  99.   tgaheadc = 0;
  100.   fwrite(&tgaheadc, sizeof(unsigned char), 1, fpo);
  101.  
  102.   /* clear empty lines */
  103.   if (rgb) {
  104.     for (pixel = 0; pixel < 256; pixel++) {
  105.       outrgb[pixel][0] = 200;
  106.       outrgb[pixel][1] = outrgb[pixel][2] = 0;
  107.     }
  108.   }
  109.   else {
  110.     for (pixel = 0; pixel < 256; pixel++)
  111.       inline[pixel] = 0;
  112.   }
  113.   for (line = 0; line < (256-nlines)/2; line++) {
  114.     if (rgb)
  115.       fwrite(outrgb, sizeof(unsigned char), 3*256, fpo);
  116.     else
  117.       fwrite(inline, sizeof(unsigned char), 256, fpo);
  118.   }
  119.  
  120.   /* read/write one line at a time */
  121.   for (line = 0; line < nlines; line++) {
  122.     if (fread(inline, sizeof(unsigned char), 256, fpi) != 256) {
  123.       printf("Error reading file %s\n", fni);
  124.       exit(1);
  125.     }
  126.     if (rgb) {
  127.       for (pixel = 0; pixel < 256; pixel++) {
  128.         outrgb[pixel][0] = (inline[pixel] == 0) ? 200 : inline[pixel];
  129.         outrgb[pixel][1] = outrgb[pixel][2] = inline[pixel];
  130.       }
  131.       fwrite(outrgb, sizeof(unsigned char), 3*256, fpo);
  132.     }
  133.     else
  134.       fwrite(inline, sizeof(unsigned char), 256, fpo);
  135.   }
  136.  
  137.   /* clear empty lines */
  138.   if (rgb)
  139.     for (pixel = 0; pixel < 256; pixel++) {
  140.       outrgb[pixel][0] = 200;
  141.       outrgb[pixel][1] = outrgb[pixel][2] = 0;
  142.     }
  143.   else
  144.     for (pixel = 0; pixel < 256; pixel++)
  145.       inline[pixel] = 0;
  146.   for (line = 0; line < (257-nlines)/2; line++)
  147.     if (rgb)
  148.       fwrite(outrgb, sizeof(unsigned char), 3*256, fpo);
  149.     else
  150.       fwrite(inline, sizeof(unsigned char), 256, fpo);
  151.  
  152.   fclose(fpi);
  153.   fclose(fpo);
  154.  
  155. }